//格式化树数据
export function toTreeData(data: any, children: string = "children") {
  let pos = {};
  let tree = [];
  let i = 0;
  while (data.length != 0) {
    if (data[i].pid == null) {
      const item = { ...data[i] };
      item[children] = [];
      tree.push(item);
      (pos as any)[data[i].id] = [tree.length - 1];
      data.splice(i, 1);
      i--;
    } else {
      let posArr = (pos as any)[data[i].pid];
      if (posArr != undefined) {
        let obj = tree[posArr[0]];
        for (let j = 1; j < posArr.length; j++) {
          obj = obj.children[posArr[j]];
        }
        const item = { ...data[i] };
        item[children] = [];
        (obj.children as any).push(item);
        (pos as any)[data[i].id] = posArr.concat([obj.children.length - 1]);
        data.splice(i, 1);
        i--;
      }
    }
    i++;
    if (i > data.length - 1) {
      i = 0;
    }
  }
  return tree;
}
